/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package connection;

/**
 *
 * @author ozgencetinkaya
 */
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import com.google.gson.Gson;
import model.UstreamBean;
 
public class ApiProxyClient {
 
        private HttpURLConnection conn;
        private Gson gson ;   
        private URL url;
        
        public ApiProxyClient(String ustreamUserName) throws IOException{
            
            gson = new Gson();
            
            String user_url = "http://api.ustream.tv/json/channel/";
            user_url += ustreamUserName + "/getInfo";
            
            url = new URL(user_url);
            
        }
        
        public UstreamBean getInfo() throws IOException{
            
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("GET");
	    conn.setRequestProperty("Accept", "application/json");
            
            if (conn.getResponseCode() != 200) {
			throw new RuntimeException("Failed : HTTP error code : "
					+ conn.getResponseCode());
		}
 
		BufferedReader br = new BufferedReader(new InputStreamReader(
			(conn.getInputStream())));
                
              return gson.fromJson(br, UstreamBean.class);
        }
    
	// http://localhost:8080/RESTfulExample/json/product/get
	public static void main(String[] args) {
 
	  try {
 
		URL url = new URL("http://api.ustream.tv/json/channel/ozgencetinkaya/getInfo");
		HttpURLConnection conn = (HttpURLConnection) url.openConnection();
		conn.setRequestMethod("GET");
		conn.setRequestProperty("Accept", "application/json");
 
		if (conn.getResponseCode() != 200) {
			throw new RuntimeException("Failed : HTTP error code : "
					+ conn.getResponseCode());
		}
 
		BufferedReader br = new BufferedReader(new InputStreamReader(
			(conn.getInputStream())));
                
		String output;
		//System.out.println("Output from Server .... \n");
		/*while ((output = br.readLine()) != null) {
			//System.out.println(output);
		}*/
                Gson gson = new Gson();
                UstreamBean obj = gson.fromJson(br, UstreamBean.class);
 
                System.out.println(obj.getResults().getId());
                
		conn.disconnect();
 
	  } catch (MalformedURLException e) {
 
		e.printStackTrace();
 
	  } catch (IOException e) {
 
		e.printStackTrace();
 
	  }
 
	}
 
}
